﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sqlsearcher.Models
{
    public class AppConst
    {
        public static string versionPath = Application.StartupPath + "\\version.txt";

        public const string JingLComputer = "计算机常见命令";
        public static int jinglCount = 0;
        public static Dictionary<int, string> computers = null;
        public static Dictionary<int, string> CExtends = null;
        public static Dictionary<int, EventHandler> JinglAc = null;
        public static Dictionary<int, EventHandler> CExtendsT3Ac = null;
        public static Dictionary<int, string> sqls = null;
        public static Dictionary<int, string> jingl = null;
        public static Dictionary<int, EventHandler> computersAc = null;
        public static Dictionary<int, EventHandler> CExtendsAc = null;
        public static Dictionary<int, Func<string>> sqlsAc = null;
        public static Dictionary<int, string> zhengzeValues = null;

        public static Dictionary<int, string> allEnums = null;
        public static string source_bianqian = "bianqian";
        public static void InitHelpers()
        {
            InitAllEnums();
            jingl = EnumHelper.GetStatus<AppEnum.Jingl_0>(new int[] { (int)AppEnum.Jingl_0.txt });
            computers = EnumHelper.GetStatus<AppEnum.Computer_1>();
            CExtends = EnumHelper.GetStatus<AppEnum.CExtends_3>();
            if (Directory.Exists(Application.StartupPath + "/sql/"))
            {
                sqlsAc = new Dictionary<int, Func<string>>();
                sqls = new Dictionary<int, string>();
                var sqlFiles = Directory.GetFiles(Application.StartupPath + "\\sql");
                var identityCount = (int)AppEnum.Jingl_0.commonsql * 100;
                for (int i = 0; i < sqlFiles.Length ; i++)
                {
                    sqls[(i+identityCount)] = Path.GetFileNameWithoutExtension(sqlFiles[i].Replace(Application.StartupPath + "\\sql\\", ""));
                    allEnums[(i + identityCount)] = Path.GetFileNameWithoutExtension(sqlFiles[i].Replace(Application.StartupPath + "\\sql\\", ""));
                    InitSqlActions((i + identityCount));
                }

            }

            InitZhengze();
           
            computersAc = new Dictionary<int, EventHandler>();
            CExtendsAc = new Dictionary<int, EventHandler>();
            CExtendsT3Ac = new Dictionary<int, EventHandler>();
            JinglAc = new Dictionary<int, EventHandler>();
        }

        private static void InitAllEnums()
        {
            allEnums = new Dictionary<int, string>();
            
            var jinglArr = EnumHelper.GetStatus<AppEnum.Jingl_0>();
            var cxpArr = EnumHelper.GetStatus<AppEnum.CExtends_3>();
            var cptArr = EnumHelper.GetStatus<AppEnum.Computer_1>();
            var zzArr = EnumHelper.GetStatus<AppEnum.ZhengZe_302>();
            foreach (var item in jinglArr.Keys)
            {
                allEnums[item] = jinglArr[item];
            }
            foreach (var item in cxpArr.Keys)
            {
                allEnums[item] = cxpArr[item];
            }
            foreach (var item in cptArr.Keys)
            {
                allEnums[item] = cptArr[item];
            }
            foreach (var item in zzArr.Keys)
            {
                allEnums[item] = zzArr[item];
            }
        }

        public static void InitActions<T>(int enumvalue, EventHandler fn) where T: Enum
        {
            if (typeof(T).FullName.IndexOf(nameof(AppEnum.Computer_1))!=-1)
            {
                computersAc[enumvalue] = fn;
            }
            if (typeof(T).FullName.IndexOf(nameof(AppEnum.CExtends_3)) != -1)
            {
                CExtendsAc[enumvalue] = fn;
            }
            if (typeof(T).FullName.IndexOf(nameof(AppEnum.ZhengZe_302)) != -1)
            {
                CExtendsT3Ac[enumvalue] = fn;
            }
            if (typeof(T).FullName.IndexOf(nameof(AppEnum.Jingl_0)) != -1)
            {
                JinglAc[enumvalue] = fn;
            }
        }

        private static void InitSqlActions(int enumvalue) 
        {
            var sql = "";
            if (!sqls.ContainsKey(enumvalue))
                return ;
            var path = Application.StartupPath + "\\sql\\" + sqls[enumvalue] + ".sql";
            if (File.Exists(path))
                sql = new StreamReader(path).ReadToEnd();
            if (allEnums == null)
                allEnums = new Dictionary<int, string>();

            
            sqlsAc[enumvalue] = () => sql;
        
        }

        private static void InitZhengze()
        {
            zhengzeValues = EnumHelper.GetStatus<AppEnum.ZhengZe_302>();
        }
    }
}
